﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PointAsLine
{
    internal class NearestNeighborSegmentation
    {
        internal static List<Polyline> Segmentation(List<List<PointF2G>> pointGroups, int maxDistance)
        {
            var allPoints=new List<PointF2G>();
            for (var i = 0; i < pointGroups.Count; i++)
            {
                allPoints.AddRange(pointGroups[i]);
            }
            return Segmentation(allPoints.ToArray(), maxDistance);
        }
        /// <summary>
        /// 使用最近邻连接法将点集分割成多个线段
        /// </summary>
        /// <param name="points">点集</param>
        /// <param name="maxDistance">两点之间的最大距离，超过则断开</param>
        /// <returns>多个线段，每个线段是一个 PointF 列表</returns>
        public static List<Polyline> Segmentation(PointF2G[] points, float maxDistance)
        {
            List<Polyline> segments = new List<Polyline>();
            bool[] visited = new bool[points.Length];

            for (int i = 0; i < points.Length; i++)
            {
                if (visited[i]) continue;

                Dictionary<int, int> groupIndexCount = new Dictionary<int, int>();
                List<PointF2G> currentSegment = new List<PointF2G>();
                currentSegment.Add(points[i]);
                visited[i] = true;

                int last = i;
                while (true)
                {
                    int next = -1;
                    float minDist = float.MaxValue;

                    // 寻找距离 last 最近的未访问点
                    for (int j = 0; j < points.Length; j++)
                    {
                        if (visited[j]) continue;

                        float dist = PointFFunc.Distance(points[last], points[j]);
                        if (dist < minDist && dist <= maxDistance)
                        {
                            minDist = dist;
                            next = j;
                        }
                    }

                    if (next == -1) break; // 没有可连接的点了

                    currentSegment.Add(points[next]);
                    if (groupIndexCount.ContainsKey(points[next].GroupIndex) == false)
                    {
                        groupIndexCount.Add(points[next].GroupIndex, 1);
                    }
                    else
                    {
                        groupIndexCount[points[next].GroupIndex]++;
                    }
                    visited[next] = true;
                    last = next;
                }

                int maxgGroupIndex = 0;
                int maxGroupCount = 0;
                foreach (var ky in groupIndexCount)
                {
                    if (ky.Value > maxGroupCount)
                    {
                        maxgGroupIndex = ky.Key;
                        maxGroupCount=ky.Value;
                    }
                }

                segments.Add(new Polyline() { Points = currentSegment,GroupIndex= maxgGroupIndex });
            }

            return segments;
        }

        
    }
}
